import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/file_util.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/modules/application/basic_information/equipment_facilities/equipment_list/equipment_details/equipment_details_controller.dart';

/// 设备设施详情
class EquipmentDetailsPage extends StatelessWidget {
  EquipmentDetailsPage({super.key});

  final controller = Get.put(EquipmentDetailsController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            appBar: CustomAppBar(
              titleText: '设备/设施详情',
            ),
            body: Container(
              alignment: Alignment.topCenter,
              color: const Color(0xffF5F5F6),
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    Container(
                      height: 38.w,
                      alignment: Alignment.centerLeft,
                      padding: EdgeInsets.only(left: 16.w),
                      child: Text('基础信息', style: TextStyle(fontSize: 12.sp, color: const Color(0xFF7C7D93))),
                    ),
                    Container(
                      height: 2.w,
                      color: const Color(0xFF587DF5),
                    ),
                    Visibility(
                      visible: controller.facilityEntity.id!.isNotEmpty,
                      child: Container(
                        color: Colors.white,
                        padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 15.w),
                        child: SingleChildScrollView(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              _getTextView(title: '设备名称：', value: controller.facilityEntity.equipName!),
                              AppGaps.vGap10,
                              _getTextView(title: '设备编码：', value: controller.facilityEntity.equipCode!),
                              AppGaps.vGap10,
                              _getTextView(title: '设备类型：', value: controller.facilityEntity.equipTypeDicIdLabel!),
                              AppGaps.vGap10,
                              _getTextView(title: '设备等级：', value: controller.facilityEntity.equipLevelLabel!),
                              AppGaps.vGap10,
                              _getTextView(
                                  title: '是否投用：',
                                  value: controller.facilityEntity.equipStatus! == '0' ? '否' : '是',
                                  valueColor: controller.facilityEntity.equipStatus! == '0' ? const Color(0xffFD6B6B) : const Color(0xff5C6DFF)),
                              AppGaps.vGap10,
                              _getDetailView()
                            ],
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          );
        });
  }

  /// 根据字典类型显示不同装置字段
  Widget _getDetailView() {
    switch (controller.facilityEntity.equipTypeDicId) {
    // 摄像头
      case 'b4512ca4-7e39-4955-86f9-0965ece7fa23':
        return _getCameraView();
    // 罐
      case '79537d04-13fb-49d2-bfe0-de80bfb45ba1':
        return _getTankView();
    // 装置
      case '00f00d6e-79e7-4406-928d-21ad3ab29884':
        return _getEquipmentView();
    // 气体检测
      case '4051d62e-3488-4137-9ae1-051c219c7c3a':
        return _getGasDetectionView();
    // 仓库或库区
      case '10f8742f-b846-4fab-822c-6e18cee323c7':
        return _getWarehouseView();
    // 站台
      case 'f09e3c95-f968-4e37-a3b8-6a174df9c140':
        return _getStationView();
      default:
        return Container();
    }
  }

  /// 摄像头类型
  Widget _getCameraView() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _getTextView(title: '危险源编码：', value: controller.facilityEntity.hazardCode!),
        AppGaps.vGap10,
        _getTextView(title: '经度：', value: controller.facilityEntity.longitude!),
        AppGaps.vGap10,
        _getTextView(title: '纬度：', value: controller.facilityEntity.latitude!),
        AppGaps.vGap10,
        _getFieldView(title: '设备描述:', value: controller.facilityEntity.equipDescribe),
        AppGaps.vGap10,
        _getFileView()
      ],
    );
  }

  /// 罐类型
  Widget _getTankView() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _getTextView(title: '罐类型：', value: controller.facilityEntity.tankTypeDicIdLabel!),
        AppGaps.vGap10,
        _getTextView(title: '压力类型：', value: controller.facilityEntity.pressureTypeDicIdLabel!),
        AppGaps.vGap10,
        _getTextView(
          title: '是否为压力容器：',
          value: controller.facilityEntity.isPressureVessel == '0' ? '否' : '是',
        ),
        AppGaps.vGap10,
        _getTextView(title: '设计压力：', value: '${controller.facilityEntity.pressureDesign}'),
        AppGaps.vGap10,
        _getTextView(title: '最高工作压力：', value: '${controller.facilityEntity.pressureMax}'),
        AppGaps.vGap10,
        _getTextView(title: '温度类型：', value: '${controller.facilityEntity.temperatureTypeDicIdLabel}'),
        AppGaps.vGap10,
        _getTextView(title: '设计温度低限：', value: '${controller.facilityEntity.temperatureMin}'),
        AppGaps.vGap10,
        _getTextView(title: '设计温度高限：', value: '${controller.facilityEntity.temperatureMax}'),
        AppGaps.vGap10,
        _getTextView(title: '存储介质：', value: '${controller.facilityEntity.medium}'),
        AppGaps.vGap10,
        _getTextView(title: '介质形态：', value: '${controller.facilityEntity.mediumStatusDicIdLabel}'),
        AppGaps.vGap10,
        _getTextView(title: '储量：', value: '${controller.facilityEntity.reserves}'),
        AppGaps.vGap10,
        _getTextView(title: '液位最高值：', value: '${controller.facilityEntity.liquidLevel}'),
        AppGaps.vGap10,
        _getTextView(title: '建造日期：', value: '${controller.facilityEntity.buildDate}'),
        AppGaps.vGap10,
        _getTextView(title: '投用日期：', value: '${controller.facilityEntity.useDate}'),
        AppGaps.vGap10,
        _getFieldView(title: '设备描述:', value: controller.facilityEntity.equipDescribe),
        AppGaps.vGap10,
        _getFileView()
      ],
    );
  }

  /// 装置类型
  Widget _getEquipmentView() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _getTextView(
          title: '是否为压力容器：',
          value: controller.facilityEntity.isPressureVessel == '0' ? '否' : '是',
        ),
        AppGaps.vGap10,
        _getTextView(
          title: '是否有压力管道：',
          value: controller.facilityEntity.isPressureVessel == '0' ? '否' : '是',
        ),
        AppGaps.vGap10,
        _getFieldView(title: '所属工艺:', value: controller.facilityEntity.chemicalProcessTypeDicIdLabel),
        AppGaps.vGap10,
        _getTextView(
          title: '是否是锅炉：',
          value: controller.facilityEntity.isBoiler == '0' ? '否' : '是',
        ),
        AppGaps.vGap10,
        _getFieldView(title: '安全仪表联锁回路总数:', value: '${controller.facilityEntity.interlockingCount}'),
        AppGaps.vGap10,
        _getFieldView(title: '安全仪表联锁回路旁路数的指标编码:', value: '${controller.facilityEntity.bypassSwitchTotalLocaleNo}'),
        AppGaps.vGap10,
        _getFieldView(title: '设备描述:', value: controller.facilityEntity.equipDescribe),
        AppGaps.vGap10,
        _getFileView()
      ],
    );
  }

  /// 气体检测类型
  Widget _getGasDetectionView() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _getTextView(title: '传感器类型：', value: '${controller.facilityEntity.monitorTypeDicIdLabel}'),
        AppGaps.vGap10,
        _getTextView(title: '监测气体：', value: controller.facilityEntity.monitorGasLabel),
        AppGaps.vGap10,
        _getFieldView(title: '设备描述:', value: controller.facilityEntity.equipDescribe),
        AppGaps.vGap10,
        _getFileView(),
      ],
    );
  }

  /// 仓库或库区类型
  Widget _getWarehouseView() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _getTextView(title: '建造日期：', value: '${controller.facilityEntity.buildDate}'),
        AppGaps.vGap10,
        _getTextView(title: '投产日期：', value: '${controller.facilityEntity.startDate}'),
        AppGaps.vGap10,
        _getTextView(title: '占地面积(平方米)：', value: '${controller.facilityEntity.floorArea}'),
        AppGaps.vGap10,
        _getTextView(title: '仓库类型：', value: '${controller.facilityEntity.storeTypeDicIdLabel}'),
        AppGaps.vGap10,
        _getTextView(title: '设计储量(吨)：', value: '${controller.facilityEntity.designReserve}'),
        AppGaps.vGap10,
        _getFieldView(title: '设备描述:', value: controller.facilityEntity.equipDescribe),
        AppGaps.vGap10,
        _getFileView(),
      ],
    );
  }

  /// 装卸站台类型
  Widget _getStationView() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _getTextView(title: '建造日期：', value: '${controller.facilityEntity.buildDate}'),
        AppGaps.vGap10,
        _getTextView(title: '投产日期：', value: '${controller.facilityEntity.startDate}'),
        AppGaps.vGap10,
        _getTextView(
          title: '是否有压力管道：',
          value: controller.facilityEntity.isPressureVessel == '0' ? '否' : '是',
        ),
        AppGaps.vGap10,
        _getTextView(title: '站台类型：', value: '${controller.facilityEntity.storeTypeDicIdLabel}'),
        AppGaps.vGap10,
        _getFieldView(title: '设备描述:', value: controller.facilityEntity.equipDescribe),
        AppGaps.vGap10,
        _getFileView(),
      ],
    );
  }

  /// 获取一行字段显示
  Widget _getTextView({required String title, required String value, Color? valueColor}) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          width: 120.w,
          child: Text(
            title,
            style: TextStyle(
              fontSize: 13.sp,
              height: 1.2,
              fontWeight: FontWeight.w400,
              color: const Color(0xffA0A7B5),
            ),
          ),
        ),
        Expanded(
            child: Text(
              value,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: valueColor ?? const Color(0xff495061),
              ),
            ))
      ],
    );
  }

  /// 获取字段样式显示
  Widget _getFieldView({required String title, String? value}) {
    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      Text(
        title,
        style: TextStyle(
          fontSize: 13.sp,
          height: 1.2,
          fontWeight: FontWeight.w400,
          color: const Color(0xffA5A6B5),
        ),
      ),
      AppGaps.vGap8,
      Container(
        alignment: Alignment.topLeft,
        padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 10.w),
        decoration: BoxDecoration(
            color: const Color(0xffF7F7FB),
            border: Border.all(color: const Color(0xffEBEDF0), width: 1.w),
            borderRadius: BorderRadius.all(Radius.circular(4.r))),
        child: Text(
          value ?? '',
          style: TextStyle(
            fontSize: 14.sp,
            height: 1.2,
            fontWeight: FontWeight.w400,
            color: const Color(0xff495061),
          ),
        ),
      )
    ]);
  }

  /// 文件显示
  Widget _getFileView() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          width: 120.w,
          child: Text(
            '文件:',
            style: TextStyle(
              fontSize: 13.sp,
              height: 1.2,
              fontWeight: FontWeight.w400,
              color: const Color(0xffA0A7B5),
            ),
          ),
        ),
        AppGaps.vGap8,
        Column(
          children: controller.facilityEntity.filePath.map(_getAttachmentView).toList(),
        ),
      ],
    );
  }

  /// 获取附件列显示
  Widget _getAttachmentView(String url) {
    //本地文件名
    final String currUrlName = url.toString().substring(25);
    return Container(
      margin: EdgeInsets.only(bottom: 10.w),
      child: Text(
        currUrlName,
        style: TextStyle(
          fontSize: 13.sp,
          color: const Color(0xFF5C6DFF),
        ),
      ).clickOnce(() {
        //打开文件
        FileUtils.openFile(Interface.fileUrl + url);
      }),
    );
  }
}
